{{!

  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}
{{#import "../python/common/typehints" as typehints}}

{{#let export partial field_patch_type |field parent|}}
{{#pragma ignore-newlines}}
{{#if (or parent.union? field.optional?)}}Optional{{#else}}Unqualified{{/if}}
FieldPatch
{{/let partial}}

{{#let export partial unadapted_pep484_patch_type |type| captures |typehints|}}
{{#pragma ignore-newlines}}
{{#if type.bool?}}BoolPatch
{{#else if type.byte?}}BytePatch
{{#else if type.i16?}}I16Patch
{{#else if type.i32?}}I32Patch
{{#else if type.i64?}}I64Patch
{{#else if type.double?}}DoublePatch
{{#else if type.float?}}FloatPatch
{{#else if type.string?}}StringPatch
{{#else if type.binary?}}BinaryPatch
{{#else if type.void?}}None
{{#else if type.enum?}}EnumPatch

  {{#else if type.structured?}}
    {{#let structured = type.true_type}}
{{#if structured.need_patch_module_path?}}{{structured.patch_module_path}}.{{/if}}{{structured.py_name}}Patch
  {{#else if type.list?}}
ListPatch[{{#partial typehints.unadapted_type type=type.true_type.elem_type}}]
  {{#else if type.set?}}
SetPatch[{{#partial typehints.unadapted_type type=type.true_type.elem_type}}]
  {{#else if type.map?}}
    {{#let map = type.true_type}}
MapPatch[
{{#partial typehints.unadapted_type type=map.key_type}}, {{!}}
{{#partial typehints.unadapted_type type=map.val_type}}, {{!}}
{{#partial unadapted_pep484_patch_type type=map.val_type}}
]
  {{/if type.bool?}}
{{/let partial}}

{{#let export partial callback |type| captures |unadapted_pep484_patch_type|}}
  {{#if type.bool?}}
return patch.as_bool_patch()
  {{#else if type.byte?}}
return patch.as_byte_patch()
  {{#else if type.i16?}}
return patch.as_i16_patch()
  {{#else if type.i32?}}
return patch.as_i32_patch()
  {{#else if type.i64?}}
return patch.as_i64_patch()
  {{#else if type.float?}}
return patch.as_float_patch()
  {{#else if type.double?}}
return patch.as_double_patch()
  {{#else if type.string?}}
return patch.as_string_patch()
  {{#else if type.binary?}}
return patch.as_binary_patch()
  {{#else if type.enum?}}
return patch.as_enum_patch()
  {{#else if type.list?}}
return ListPatch(patch.as_list_patch(), type_info)
  {{#else if type.set?}}
return SetPatch(patch.as_set_patch(), type_info)
  {{#else if type.map?}}
    {{#let val_type = type.true_type.val_type}}
  {{! e.g., if map value is an i32, this function convert DynamicPatch --> I32Patch }}
def cast_dynamic_patch_to_typed_map_value_patch(patch: DynamicPatch, type_info) -> {{#partial unadapted_pep484_patch_type type=val_type}}:
    {{#partial callback type=val_type}}
return MapPatch(cast_dynamic_patch_to_typed_map_value_patch, patch.as_map_patch(), type_info)
  {{#else if type.structured?}}
    {{#let struct = type.true_type}}
return {{#if struct.need_patch_module_path?}}{{struct.patch_module_path}}.{{/if}}{{struct.py_name}}Patch(patch)
  {{/if type.bool?}}
{{/let partial}}
